//
//  PostPhotoView.m
//  wtshoppingcite
//
//  Created by Baojia-LC on 14/11/4.
//  Copyright (c) 2014年 TW. All rights reserved.
//

#import "PostPhotoView.h"

static NSInteger _num;
@implementation PostPhotoView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        self.backgroundColor = COLOR_RGB(239, 239, 239);
        _imageVs = [[NSMutableArray alloc] initWithCapacity:0];
        [self createSubView];
    }
    return self;
}



-(void)createSubView{
    
    _imageArray = [[NSMutableArray alloc]initWithCapacity:1];
    
    _photoImageView = [[PhotFacImage alloc] initWithFrame:CGRectMake(10, 5, self.height - 10, self.height - 10)];
    [_photoImageView setImage:[UIImage imageNamed:@"tianjiaxuanxiang"] forState:UIControlStateNormal];
    _photoImageView.layer.cornerRadius = 5;
    _photoImageView.clipsToBounds = YES;
    _photoImageView.t_delegate = self;
    [_photoImageView addTarget:self action:@selector(photoClick:) forControlEvents:UIControlEventTouchUpInside];
    __weak PostPhotoView* this = self;
    _photoImageView.block = ^(PhotFacImage* button){
        [this deletePhotoAndServers:button];
    };
    _photoImageView.tag =100;
    [self addSubview:_photoImageView];
    [_imageVs addObject:_photoImageView];
    
    _label = [[UILabel alloc] initWithFrame:CGRectMake(_photoImageView.right+10, (self.height - 20)/2, 120, 20)];
    _label.text = @"点击上传照片";
    _label.hidden = NO;
    _label.font = [UIFont boldSystemFontOfSize:TITLESMALL_FONT];
    _label.textColor = COLOR_RGB(164, 164, 164);
    _label.backgroundColor = [UIColor clearColor];
    [self addSubview:_label];
    
}

//上传图片
-(void)photoClick:(PhotFacImage*)button{
    
    _selectPhotoImageView = button;
    _label.hidden = YES;

    UIActionSheet* actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"拍照",@"从相册选择", nil];
    actionSheet.tag = 10;
    [actionSheet showInView:self];
    
    if(_tapBlock != nil)
        _tapBlock(button);

}


- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    if(actionSheet.tag == 10){
        
        //拍照
        if(buttonIndex == 0){
            
            [self photographByCarema];
 
        }
        //从相册选择
        else if(buttonIndex == 1){
            
            [self pirtureGetToCarema];
            
        }else if(buttonIndex == 2){
            actionSheet.hidden = YES;
        }
    }else{
        
    }
}

//拍照
-(void)photographByCarema{
    
    UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.allowsEditing = YES;//允许编辑，只有框里面的才显示
    imagePickerController.delegate = self;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
        imagePickerController.sourceType = UIImagePickerControllerSourceTypeCamera;
        [self.viewController presentViewController:imagePickerController animated:YES completion:nil];
    }
    
}

//从相册选择
-(void)pirtureGetToCarema{
    
    UIImagePickerController* imagePickerController = [[UIImagePickerController alloc] init];
    imagePickerController.allowsEditing = YES;//允许编辑，只有框里面的才显示
    imagePickerController.delegate = self;
    imagePickerController.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [self.viewController  presentViewController:imagePickerController animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
    
    UIImage *image1 = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
    
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        [UIImage saveImageToAlbum:image1] ;
    }
    
    [_imageArray addObject:image1];
    
#pragma TODO 删除操作保存存到服务器
//    if(self.photoImageView.isHaveImage){
//                [self deletePhotoAndServers];
//    }
#pragma TODO 上传操作保存到服务器
//    else{
            [self postImage:image1] ;
//    }
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    //
}

- (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker

{
#pragma todo
    //    TipCarShowImageView* imgV = (TipCarShowImageView*)[self.scrollview viewWithTag:self.selectedImageView.tag];
    //    imgV.image = [UIImage imageNamed:[NSString stringWithFormat:@"DCar%d.png",_selectedImageView.tag]];
    
    [picker dismissViewControllerAnimated:YES completion:nil];
    
}

//删除图片操作
-(void)deletePhotoAndServers:(PhotFacImage*)button{

//    PhotFacImage* nextPhotImageV = (PhotFacImage*)[self viewWithTag:button.tag+1];
    PhotFacImage* pf = (PhotFacImage*)[self viewWithTag:button.tag];
//    if(nextPhotImageV != nil){
//        nextPhotImageV.left = photImageV.left;
//    }else{
//        nextPhotImageV.backgroundColor = MAIN_GRAY_COLOR;
//        [nextPhotImageV setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
//        return;
//    }
    for(int i = button.tag - 100;i<_imageVs.count;i++){
        PhotFacImage* nextPhotImageV = (PhotFacImage*)[self viewWithTag:i+100+1];
        PhotFacImage* prefexPhotImageV = (PhotFacImage*)[self viewWithTag:i-1+100];
//        if(nextPhotImageV != nil){
        if(prefexPhotImageV == nil){
            
        }
            nextPhotImageV.left = 90*i+10;
//        }
    }

    if(_imageVs.count ==1){
        pf.backgroundColor = MAIN_GRAY_COLOR;
        [pf setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        return;
    }
    [pf removeFromSuperview];
    pf = nil;
    
    [_imageVs removeObjectAtIndex:button.tag-100];
    _num--;

}

//加下一张图片操作
-(void)postImage:(UIImage*)image{

    NSLog(@"%@",_imageVs);
    PhotFacImage* photImageV = (PhotFacImage*)[self viewWithTag:_selectPhotoImageView.tag];
    [photImageV setImage:image forState:UIControlStateNormal];
    if(_selectPhotoImageView.isHaveImage == YES){
        return;
    }
    photImageV.isHaveImage = YES;

    if(_imageVs.count>=3){
        return;
    }
    _num++;

    PhotFacImage* photoImageView = [[PhotFacImage alloc] initWithFrame:CGRectMake(_selectPhotoImageView.right + 10, 5, self.height - 10, self.height - 10)];
    [photoImageView setImage:[UIImage imageNamed:@"tianjiaxuanxiang"] forState:UIControlStateNormal];
    photoImageView.layer.cornerRadius = 5;
    photoImageView.t_delegate = self;
    photoImageView.clipsToBounds = YES;
    [photoImageView addTarget:self action:@selector(photoClick:) forControlEvents:UIControlEventTouchUpInside];
    __weak PostPhotoView* this = self;
    photoImageView.block = ^(PhotFacImage* button){
        [this deletePhotoAndServers:button];
    };
    photoImageView.tag = _num+100;
    [self addSubview:photoImageView];

    [_imageVs addObject:photoImageView];

}

#pragma mark - custom delegate
- (void) tappedWithObject:(id)sender
{
    PhotFacImage *photFacImage = (PhotFacImage *)sender;
    if ([photFacImage.imageView.image isEqual:[UIImage imageNamed:@"tianjiaxuanxiang"]]) {
        [self photoClick:photFacImage];
        return;
    }
    
    if (self.delegate && [self respondsToSelector:@selector(tappedWithObject:)]) {
        [self.delegate tappedWithObject:sender];
    }
    
}


@end
